/**
 * \class CSpaceProxy
 * \brief This is a space proxy to provide a transparent view of distributed spaces to users using write/read/take.
 * \version 0.3.2
 *
 * \author Woo-Hyun Kim (woorung@nhncorp.com, woorung@gmail.com)
 * \author http://blog.naver.com/wisereign .
 *
 * \date 2007/10/9
 */
#ifndef CSpaceProxyH
#define CSpaceProxyH
//-----------------------------------------------------------------
#include <iostream>
#include <vector>
#include <map>
#include <iterator>

#include "md5/CMD5.h"

#include "common_space.h"
#include "CDHT.h"

#include "CSpaceMonitorThread.h"

using namespace std;

// avoid cross reference
//-----------------------------------------------------------------
class CWorkSpaces;
//-----------------------------------------------------------------

class CSpaceProxy
{
private:
	string local_ip;

protected:
	string _service;
	string _instance;
	CDHT *_topology;

	int workspace(CWorkSpaces *wspace);

	char *getLocalIP();

	int randomly(CServiceInfo &randomly_space);
	int biggest(CServiceInfo &biggest_space);
	int onebyone(CServiceInfo &biggest_space);

	int read(string &space_key, pair<string,string> &kv, char *cmd);
	int take(string &space_key, pair<string,string> &kv, char *cmd);

public:
	CSpaceProxy();
	CSpaceProxy(CWorkSpaces *wspace);
	virtual ~CSpaceProxy();

	virtual int write(pair<string,string> &kv);
	virtual int write(string &space_key, pair<string,string> &kv);
	virtual int read(pair<string,string> &kv);
	virtual int read(string &space_key, pair<string,string> &kv);
	virtual int readIfExists(pair<string,string> &kv);
	virtual int readIfExists(string &space_key, pair<string,string> &kv);
	virtual int take(pair<string,string> &kv);
	virtual int take(string &space_key, pair<string,string> &kv);
	virtual int takeIfExists(pair<string,string> &kv);
	virtual int takeIfExists(string &space_key, pair<string,string> &kv);

	// get size of all the data from local spaces
	long size();
	// get size of the data from a specific space
	long size(string &space_key);

	// remove space service's instance
	int clean(string &space_key);
};
//-----------------------------------------------------------------
#endif
